var address = require('../../utils/cityData.js');
Component({
    properties: {
        defaul_val:{
            type: String, // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
            value: '', // 属性初始值（必填）
        }
        // 定义了name属性，可以在使用组件时，由外部传入。此变量可以直接在组件模板中使用
    },
    data: {
        value:[0,0,0],
        provinces:[],
        citys:[],
        areas:[],
        province:'',
        city:'',
        area:'',
        total_location:'',
        status:false,
    },
    attached:function(){
        this.init()
    },
    methods: {
        init:function(){ //选择器初始化
            const that = this;
            this.setData({
                provinces:address.provinces
            },function(){
                that.output(that.data.value)
            })
            console.log(this.data.defaul_val)
            if(this.data.defaul_val!=''){
                var defaul_val = this.data.defaul_val
                this.setData({
                    total_location:defaul_val
                })
            }
            this.choose_citys(address.provinces[0].id,0)
        },
        choose_citys:function(id,idx){ //选择city
            this.setData({
              citys: address.citys[id]
            })
            this.choose_areas(address.citys[id][idx]?address.citys[id][idx].id:address.citys[id][0].id)
            // console.log(address.citys[id])
        },
        choose_areas:function(id){ //选择区
            this.setData({
              areas: address.areas[id]
            })
        },
        change:function(e){
            console.log(e)
            var provinces = [];
            const that = this;
            provinces.push(address.provinces[e.detail.value[0]])
            this.setData({
                value:[e.detail.value[0],e.detail.value[0] == this.data.value[0]?e.detail.value[1]:0,e.detail.value[0] == this.data.value[0]?(e.detail.value[1] == this.data.value[1]?e.detail.value[2]:0):0]
            },function(){
                that.choose_citys(provinces[0].id,that.data.value[1])
                that.output(that.data.value)
            })
        },
        output:function(val){
            this.setData({
                province:this.data.provinces[val[0]].name,
                city:this.data.citys[val[1]]!=undefined?this.data.citys[val[1]].name:this.data.citys[0].name,
                area:this.data.areas[val[2]]!=undefined?this.data.areas[val[2]].name:''
            })
        },
        sure:function(){
            const that = this;
            this.setData({
                total_location:`${this.data.province}-${this.data.city}-${this.data.area}`
            },function(){
                var loc = {};
                loc.province = that.data.province;
                loc.city = that.data.city;
                loc.area = that.data.area;               
                that.triggerEvent('setdata',loc) //向父组件传值
            })
            this.close()
        },
        close:function(){
            this.setData({
                status:false
            })
        },
        open:function(){
            this.setData({
                status:true
            })  
        },
        setdefaul_val:function(e){
            this.setData({
                total_location:e
            })
        }
    }
});